<?php

namespace App\Http\Controllers\Auth;

use Auth;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
session_start();
class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    /**
     * Where to redirect users after login / registration.
     *
     * @var string
     */
    protected $redirectTo = '/';

    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'logout']);
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'uid'=>'required|unique:users',
            'name' => 'required|max:255',
            'type' => 'required',
            'password' => 'required|confirmed|min:6',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'uid' => $data['uid'],
            'name' => $data['name'],
            'type' => $data['type'],
            'password' => bcrypt($data['password']),
        ]);
    }

     /**
     * 处理登录认证
     *
     * @return Response
     */
    public function authenticate()
    {
        $uid=$_POST['uid'];
        $password=$_POST['password'];
            
        if (Auth::attempt(['uid' => $uid, 'password' => $password])) {
            // 认证通过...
            $user=User::where('uid','=',$uid)->first();
            $_SESSION['sid']=$uid;
            $_SESSION['name']=$user->name;

           

            switch ($user->type) {
                case 0:
                    return redirect()->intended('/std/home');
                    break;
                case 1:
                    return redirect()->intended('/thr/home');
                    break;
                case 2:
                    return redirect()->intended('/admin/home');
                    break;
                default:
                    break;
            }
        }
        else
        {
            
            #return redirect()->intended('/',['error'=>"用户名或密码错误。"]);
            return view('/auth/login',['error'=>"用户名或密码错误。"]);
        }
    }

    public function getLogout(){
        unset($_SESSION['sid']);
        unset($_SESSION['name']);
        return view('/auth/login');
    }
    
}
